package com.edu.servlet;

import com.edu.bean.CampusStyle;
import com.edu.service.UserNewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = {"/user/campus-style", "/user/campus-style/detail"})
public class UserCampusStyleServlet extends HttpServlet {
    private UserNewsService userNewsService = new UserNewsService();
    private static final int PAGE_SIZE = 9; // 每页显示9条记录

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String servletPath = request.getServletPath();
        if ("/user/campus-style".equals(servletPath)) {
            showList(request, response);
        } else if ("/user/campus-style/detail".equals(servletPath)) {
            showDetail(request, response);
        } else {
            response.sendRedirect(request.getContextPath() + "/user/home");
        }
    }

    private void showList(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取当前页码
        String pageStr = request.getParameter("page");
        int page = 1;
        try {
            if (pageStr != null && !pageStr.trim().isEmpty()) {
                page = Integer.parseInt(pageStr);
                if (page < 1) {
                    page = 1;
                }
            }
        } catch (NumberFormatException e) {
            page = 1;
        }

        // 获取数据
        List<CampusStyle> styleList = userNewsService.userGetCampusStyleList(page, PAGE_SIZE);
        int totalCount = userNewsService.userGetCampusStyleTotalCount();
        int totalPages = (totalCount + PAGE_SIZE - 1) / PAGE_SIZE;

        // 设置属性
        request.setAttribute("styleList", styleList);
        request.setAttribute("currentPage", page);
        request.setAttribute("totalPages", totalPages);

        // 转发到列表页
        request.getRequestDispatcher("/user/campus-style/list.jsp").forward(request, response);
    }

    private void showDetail(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String idStr = request.getParameter("id");
        if (idStr != null && !idStr.trim().isEmpty()) {
            try {
                int styleId = Integer.parseInt(idStr);
                CampusStyle style = userNewsService.userGetCampusStyleDetail(styleId);
                if (style != null && style.getStatus() == 1) {  // 确保风采存在且为显示状态
                    request.setAttribute("style", style);
                    request.getRequestDispatcher("/user/campus-style/detail.jsp").forward(request, response);
                    return;
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        response.sendRedirect(request.getContextPath() + "/user/campus-style");
    }
}